const md5 = require("md5");
const { validationResult } = require("express-validator");
const { addUser, updateUser, getUser } = require("../model/user");
const formatError = require("../utils/formatError");
// 用户登录
const Login = async (req, res) => {
    const result = validationResult(req);
    if (!result.isEmpty()) {
        return res.status(400).send({
            code: 400,
            msg: "参数错误",
            data: formatError(result.array()),
        });
    }
    try {
        const user = await getUser({
            username: req.body.username,
            password: md5(req.body.password),
        });
        if (!user) {
            return res.status(404).send({
                code: 404,
                msg: "用户名或密码错误",
                data: null,
            });
        }
        req.session.user = user;
        req.session.date = Date.now();
        return res.send({
            code: 200,
            msg: "Success",
            data: user,
        });
    } catch (error) {
        res.status(500).send({
            code: 500,
            msg: "Internal Server Error",
            data: null,
        });
    }
};

// 用户注册
const Register = async (req, res) => {
    const result = validationResult(req);
    if (!result.isEmpty()) {
        return res.status(400).send({
            code: 400,
            msg: "Parameter validation failed",
            data: formatError(result.array()),
        });
    }
    try {
        const { username, password } = req.body;
        const user = await addUser({ username, password: md5(password) });
        return res.send({
            code: 200,
            msg: "Success",
            data: user,
        });
    } catch (error) {
        res.status(500).send({
            code: 500,
            msg: "Internal Server Error",
            data: null,
        });
    }
};

// 更新
const Update = async (req, res) => {
    const result = validationResult(req);
    if (!result.isEmpty()) {
        return res.status(401).send({
            code: 401,
            msg: "请求参数错误",
            data: formatError(result.array()),
        });
    }
    try {
        const { id } = req.params;
        if (id !== req.session.user._id) {
            return res.status(401).send({
                code: 401,
                msg: "没有权限",
                data: null,
            });
        }
        const { username, password } = req.body;
        const data = {};
        username && (data.username = username);
        password && (data.password = md5(password));
        const result = await updateUser(id, data);
        return res.send({
            code: 200,
            msg: "更新成功",
            data: result,
        });
    } catch (error) {
        res.status(500).send({
            code: 500,
            msg: "数据库操作失败",
            data: null,
        });
    }
};

// 查询
const Detail = async (req, res) => {
    const result = validationResult(req);
    if (!result.isEmpty()) {
        return res.status(401).send({
            code: 401,
            msg: "请求参数错误",
            data: formatError(result.array()),
        });
    }
    try {
        const { id } = req.params;
        const user = await getUser({ _id: id });
        return res.send({
            code: 200,
            msg: "查询成功",
            data: user,
        });
    } catch (error) {
        res.status(500).send({
            code: 500,
            msg: "数据库操作失败",
            data: null,
        });
    }
};

// 注销
const Logout = async (req, res) => {
    req.session.destroy();
    return res.send({
        code: 200,
        msg: "注销成功",
        data: null,
    });
};

module.exports = { Login, Register, Update, Detail, Logout };
